package com.example.test;

import com.sun.tools.attach.*;
import java.io.IOException;
import java.util.List;

public class AgentAttachDemo {

    public static void main(String[] args) throws IOException, AttachNotSupportedException, AgentLoadException, AgentInitializationException, InterruptedException {
        List<VirtualMachineDescriptor> vm = VirtualMachine.list();
        for (VirtualMachineDescriptor vmd : vm) {
            if ("com.example.Application".equals(vmd.displayName())){
                System.out.println("注入探针");
                VirtualMachine virtualMachine = VirtualMachine.attach(vmd.id());
                virtualMachine.loadAgent("D:\\Code\\projects\\java-agent\\maven-agent\\target\\maven-agent.jar");
                virtualMachine.detach();
            }
        }
    }
}
